// pages/home/course/list.js
import {
  getFreeCourseList,
  getChairCourseList,
} from '../../../api/course.js';
import {
  getCourseFreeOptions,
  getCourseMasterOptions,
} from '../../../api/other.js';


Page({

  /**
   * 页面的初始数据
   */
  data: {
    isIOS: false,
    type: 0,
    navigationBarTitle: ['课程分类', '免费课程', '大咖详情'],
    currentId: 1,
    classifyId: 0,
    basicOptions: [{
      id: -1,
      name: '全部课程',
      subClassifys: [{
        id: -1,
        name: '关注热点'
      }]
    }],
    options: [],
    secondOptions: [],
    courseList: [],
    pageNo: 1,
    rowCount: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const type = Number(options.type);
    this.data.type = type;

    this.setData({
      isIOS: getApp().globalData.isIOS,
    })

    wx.setNavigationBarTitle({
      title: this.data.navigationBarTitle[type]
    })

    this.loadOptions();
  },

  /**
   * 加载课程列表
   */
  async loadCourseList() {
    let res;
    if (this.data.type === 0) {
      res = await getCourseOptionsList(this.data.classifyId, this.data.pageNo);
    } else if (this.data.type === 1) {
      res = await getFreeCourseList(this.data.classifyId, this.data.pageNo);
    } else if (this.data.type === 2) {
      res = await getChairCourseList(this.data.classifyId, this.data.pageNo);
    }
    this.setData({
      courseList: this.data.courseList.concat(res.list),
      rowCount: res.rowCount
    })
  },

  /**
   * 装填侧边栏
   */
  async loadOptions() {
    let res;
    if (this.data.type === 0) {
      res = await getCourseOptions();
    } else if (this.data.type === 1) {
      res = await getCourseFreeOptions();
    } else if (this.data.type === 2) {
      res = await getCourseMasterOptions();
    }
    this.setData({
      options: this.data.basicOptions.concat(res),
      currentId: this.data.basicOptions[0].id,
    })
    this.loadSecondOptions(this.data.basicOptions[0].id);
  },

  /**
   * 装填二级滑块
   * @param {number} id 父级 ID
   */
  loadSecondOptions(id) {
    for (let item of this.data.options) {
      if (item.id === id) {
        this.setData({
          secondOptions: item.subClassifys
        })
        this.handleSelectSecondOptions({
          detail: {
            id: item.subClassifys[0].id
          }
        })
        return;
      }
    }
  },

  /**
   * 侧边栏选中
   */
  handleChangeOption(e) {
    const {
      id
    } = e.currentTarget.dataset;
    if (id == null || this.data.currentId === id) {
      return
    }
    this.setData({
      currentId: id,
    })
    this.loadSecondOptions(id);
  },

  /**
   * 二级类目选中
   * @param {object} e 
   */
  handleSelectSecondOptions(e) {
    this.data.classifyId = e.detail.id;

    // TODO 请求数据
    this.data.pageNo = 1;
    this.data.courseList.length = 0;
    this.loadCourseList();
  },

  /**
   * 跳转详情
   */
  handleNavigatorCourse(e) {
    wx.navigateTo({
      url: '/pages/learn/course/details?id=' + e.detail,
    })
  },

  /**
   * 滚动到底部
   * @param {object} e 
   */
  handleScrollBottom(e) {
    if (this.data.rowCount > this.data.courseList.length) {
      this.data.pageNo += 1;
      this.loadCourseList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})